<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="楼座名称" prop="buildingName">
        <el-input v-model="form.buildingName" placeholder="请输入楼座名称" />
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input v-model="form.area" placeholder="请输入面积" />
      </el-form-item>
      <el-form-item label="楼层" prop="floorConfig">
        <el-input
          v-model="form.floorConfig"
          placeholder="请输入楼层名称，输入数字将生成对应数量的楼层"
        />
      </el-form-item>
      <el-form-item label="管理员姓名" prop="manageName">
        <el-input v-model="form.manageName" placeholder="请输入管理员姓名" />
      </el-form-item>
      <el-form-item label="联系电话" prop="managePhone">
        <el-input v-model="form.managePhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入详细地址" />
      </el-form-item>

      <!-- <el-form-item label="机构编号" prop="orgId">
                        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
                      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" />
                      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  createAccommodationRoom,
  updateAccommodationRoom,
} from "@/api/collegebase/accommodationRoom";
import common from "@/api/collegebase/common";
import {
  getAccommodationBuildingPage,
  createAccommodationBuilding,
  updateAccommodationBuilding
} from "@/api/collegebase/accommodationBuilding.js";
import { getAccommodationFloorPage } from "@/api/collegebase/accommodationFloor.js";

export default {
  name: "AccommodationRoomEdit",
  components: {},
  data() {
    return {
      bunkData: [{ value: "" }],
      spreadData: [{ value: "" }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingName: [
          { required: true, message: "请输入楼座名称", trigger: "blur" },
        ],
        floorConfig: [
          { required: true, message: "请输入楼层", trigger: "blur" },
        ],
        manageName: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
        managePhone: [{ required: true, message: "请输入管理员电话", trigger: "blur" }],
        roomName: [
          { required: true, message: "请输入房s间名称", trigger: "blur" },
        ],
        area: [{ required: true, message: "请输入面积", trigger: "blur" }],

      },
      departmentsList: [],
      buildList: [],
      floorList: [],
    };
  },
  created() {
    this.getData();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },

  methods: {
    async buildChange(buildingId) {
      this.$set(this.form, "floorId", "");
      const res = await getAccommodationFloorPage({
        buildingId,
        pageNo: 1,
        pageSize: 99,
      });
      this.floorList = res.data.list;
    },


    //获取学院
    async getData() {

      getAccommodationBuildingPage({ pageNo: 1, pageSize: 99 }).then((res) => {
        this.buildList = res.data.list;
      });
    },
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        serialNumber: undefined,
        roomName: undefined,
        collegeId: undefined,
        departmentId: undefined,
        floorId: undefined,
        roomArea: undefined,
        roomStatus: undefined,
        roomType: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        this.form.manageId = 'TTT202208221744387527070376830';
        this.form.area = parseFloat(this.form.area).toFixed(2);
        // 修改的提交
        if (this.form.id != null) {
          updateAccommodationBuilding(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createAccommodationBuilding(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
